package com.itheima.controller;

import com.itheima.aspect.Log;
import com.itheima.entity.*;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @GetMapping
    /*
    分页查询
     */
    public Result page(EmpQueryParam param){

        log.info("分页查询：{},{},{},{},{},{}", param.getName(),param.getGender(),param.getBegin(),param.getEnd(),param.getPage(),param.getPageSize());


        PageBean pageBean=empService.page(param);

        return Result.success(pageBean);
    }
    /*
    新增员工
     */
    @Log
    @PostMapping
    public Result save(@RequestBody Emp emp){
        log.info("新增员工：{}",emp);

        empService.save(emp);
        return Result.success();
    }
    /*
    删除员工
     */
    @Log
    @DeleteMapping
//    public Result delete(Integer[]ids){
    public Result delete(@RequestBody List<Integer> ids){
        log.info("删除员工：{}",ids);
        empService.delete(ids);
        return Result.success();
    }
    /*
    数据回显
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id){
        log.info("数据回显：{}",id);
        Emp emp = empService.getById(id);
        return Result.success(emp);
    }

    /**
     *
     * 修改员工
     *
     * @param emp
     * @return
     */
    @Log
    @PutMapping
    public Result update(@RequestBody Emp emp){
        log.info("修改员工：{}",emp);
        empService.update(emp);
        return Result.success();
    }
    /*
    查询所有员工
     */
    @GetMapping("/list")
    public Result list(){
        log.info("查询所有员工");
        List<Emp> empList = empService.list();
        return Result.success(empList);
    }
}
